package com.lanou.dllo.mynovel.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.dllo.mynovel.R;
import com.lanou.dllo.mynovel.bean.StoreRanking;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.Inflater;

/**
 * Created by dllo on 15/11/2.
 */
public class StoreRankingAdapter extends
        RecyclerView.Adapter<StoreRankingAdapter.StoreRankingHolder> {

    private List<StoreRanking.DataEntity.ReturnDataEntity.RankinglistEntity> rankinglist;
    private RequestQueue requestQueue; // 定义一个队列请求

    // 此方法内初始化请求队列
    public StoreRankingAdapter(Context context) {
        // 初始化队列请求
        requestQueue = Volley.newRequestQueue(context);
    }

    public void addData(List<StoreRanking.DataEntity.ReturnDataEntity.RankinglistEntity> datas) {
        rankinglist = datas;
        notifyDataSetChanged(); // 改变数据 通知刷新
    }

    public  class StoreRankingHolder extends RecyclerView.ViewHolder {
        private ImageView ranking_cover;
        private TextView rankingName,rankingDescription1,rankingDescription2;

        public StoreRankingHolder(View itemView) {
            super(itemView);

            ranking_cover = (ImageView) itemView.findViewById(R.id.ranking_cover);
            rankingName = (TextView) itemView.findViewById(R.id.rankingName);
            rankingDescription1 = (TextView) itemView.findViewById(R.id.rankingDescription1);
            rankingDescription2 = (TextView) itemView.findViewById(R.id.rankingDescription2);

        }
    }



    @Override
    public StoreRankingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 获得布局加载器
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        // 加载行布局文件
        View view = inflater.inflate(R.layout.fragment_storeranking_content, null);
        // 构建组件缓存对象并返回
        return new StoreRankingHolder(view);
    }

    /**
     * 通过volley请求图片
     **/
    public void getBitmap(String url, final ImageView imageView, int w, int h) {

        ImageRequest request = new ImageRequest(url,
                new Response.Listener<Bitmap>() {

                    @Override
                    public void onResponse(Bitmap response) {
                        // 将返回的bitmap图片对象设置到图片组件中
                        imageView.setImageBitmap(response);
                    }
                },
                w, h, Bitmap.Config.ARGB_8888,
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // 打印错误信息
                        error.printStackTrace();
                    }
                }) {
            @Override
            public Map<String, String> getHeaders()
                    throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        requestQueue.add(request); // 将本次图片请求加入到请求列队中

    }

    @Override
    public void onBindViewHolder(StoreRankingHolder holder, int position) {
        if (rankinglist != null && rankinglist.size() > 0) {
            StoreRanking.DataEntity.ReturnDataEntity.RankinglistEntity entity = rankinglist.get(position);
            // 设置数据
            holder.rankingName.setText(entity.getRankingName());
            holder.rankingDescription1.setText(entity.getRankingDescription1());
            holder.rankingDescription2.setText(entity.getRankingDescription2());

            if (entity.getCover() != null) {
                String imgUrl = entity.getCover();
                getBitmap(imgUrl, holder.ranking_cover, 1000, 500);
            }
        }
    }

    @Override
    public int getItemCount() {
        return rankinglist != null && rankinglist.size() > 0 ? rankinglist.size() : 0;
    }


}
